$(function () {
    var regularStu_datagrid = $("#regularStu_datagrid");
    var regularStu_dialog = $("#regularStu_dialog");
    var regularStu_add_form = $("#regularStu_add_form");
    var retention_dialog = $("#retention_dialog");

    regularStu_datagrid.datagrid({
        url: "/regularStu/query",
        fit: true,
        fitColumns: true,
        fit: true,
        rownumbers: true,
        columns: [[
            {field: "name", title: "学生姓名", width: 100},
            {field: "age", title: "年龄", width: 100},
            {field: "gender", title: "性别", width: 100, formatter: formatterGender},
            {field: "address", title: "家庭地址", width: 100},
            {field: "inputEmployee", title: "营销人员", width: 100, formatter: formatterInputEmployee},
            {field: "inputTime", title: "录入时间", width: 150},
            {field: "qq", title: "qq号码", width: 100},
            {field: "totalFee", title: "总费用", width: 100},
            {field: "unPaidFee", title: "未缴费用", width: 100},
            {field: "paidFee", title: "已缴费用", width: 100},
            {field: "feeStatus", title: "缴费状态", width: 100, formatter: formatterFeeStatus},
            {field: "payment", title: "付款方式", width: 100, formatter: formatterPayment},
            {field: "paymentType", title: "支付类型", width: 100, formatter: formatterPaymentType},
            {field: "inClassTime", title: "入学时间", width: 150},
            {field: "campus", title: "入学校区", width: 120, formatter: formatterCampus},
            {field: "classgrade", title: "所在班级", width: 230, formatter: formatterClassGrade},
            {field: "college", title: "所在学院", width: 120, formatter: formatterCollege},
            {field: "tel", title: "联系电话", width: 100},
            {field: "status", title: "状态", width: 80, formatter: formatterStatus},
            {field: "classStatus", title: "班级状态", width: 120, formatter: formatterClassStatus}
        ]],
        toolbar: "#regularStu_toolbar",
        singleSelect: true,
        pagination: true
    });
    //编辑弹框
    regularStu_dialog.dialog({
        buttons: "#regularStu_dialog_buttons",
        closed: true
    });

    //1.把方法绑定到一个对象上
    var cmdObj = {
        //正式学员编辑
        editRegularStu: function () {
            //获取选中行
            var selectedRow = regularStu_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "当前无人被选中", "error");
                return;
            }
            if(selectedRow.status == 1){
                $.messager.alert("提示","该学员已经毕业,无法编辑","error");
                return ;
            }else if(selectedRow.status == -1){
                $.messager.alert("提示","该学员已经退学,无法编辑","error");
            }else if(selectedRow.status == -2){
                $.messager.alert("提示","该学员已经休学,无法编辑","error");
            }
            //设置表单回显
            //清空表单
            regularStu_add_form.form("clear");
            //数据回显
            console.log(selectedRow);
            //性别回显
            if (selectedRow.gender == 0) {
                $("#gender_combobox").combobox("select", "0");
            } else {
                $("#gender_combobox").combobox("select", "1");
            }
            //所在班级回显
            if (selectedRow.classgrade) {
                selectedRow['classgrade.id'] = selectedRow.classgrade.id;
            }
            //营销人员回显
            if (selectedRow.inputEmployee) {
                selectedRow['inputEmployee.id'] = selectedRow.inputEmployee.id;
            }
            //所在校区回显
            if (selectedRow.campus) {
                selectedRow['campus.id'] = selectedRow.campus.id;
            }
            //所在学院回显
            if (selectedRow.college) {
                selectedRow['college.id'] = selectedRow.college.id;
            }
            //付款方式回显
            if (selectedRow.payment) {
                selectedRow['payment.id'] = selectedRow.payment.id;
            }
            //支付类型回显
            if (selectedRow.paymentType) {
                selectedRow['paymentType.id'] = selectedRow.paymentType.id;
            }
            //缴费状态回显
            if (selectedRow.feeStatus == 0) {
                $("#feeStatus_combobox").combobox("select", "0");
            } else {
                $("#feeStatus_combobox").combobox("select", "1");
            }
            regularStu_add_form.form("load", selectedRow);
            //弹出编辑框
            regularStu_dialog.dialog("setTitle", "编辑");
            $(".baseMessage").combobox('readonly', false);
            $(".baseMessage").datebox('readonly', false);
            $(".baseMessage").textbox('readonly', false);
            $("#commit_button").linkbutton('enable');
            regularStu_dialog.dialog("open");

        },
        //正式员工加载
        reloadRegularStu: function () {
            regularStu_datagrid.datagrid("reload");
        },
        //提交保存
        save: function () {
            //提交表单
            regularStu_add_form.form("submit", {
                url: "/regularStu/update",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            cmdObj.cancel();
                            cmdObj.reloadRegularStu();
                        });
                    } else {
                        $.messager.alert("提示", data.msg, "error");
                    }
                }
            });
        },
        //取消
        cancel: function () {
            //关闭添加弹窗
            regularStu_dialog.dialog("close");
        },
        //高级查询
        search: function () {
            //1.获取查询input里面的值
            var keyWord = $("#keyWord").val();
            var className = $("#className").val();
            var beginDate = $("#beginDate").val();
            var endDate = $("#endDate").val();
            var classStatus = $("#classStatus").val();
            var status = $("#status").val();

            //2.调用datagrid重新加载数据,并且带上查询条件  keyword className beginDate endDate
            regularStu_datagrid.datagrid("load", {
                keyWord: keyWord,
                className: className,
                beginDate: beginDate,
                endDate: endDate,
                classStatus: classStatus,
                status: status
            });
        },
        //退学
        removeStudyRegularStu: function () {
            //获取选中行
            var selectedRow = regularStu_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "当前无人被选中", "error");
                return;
            }
            //判断当前学员状态
            if (selectedRow.status == -2) {
                $.messager.alert("提示", "学员[" + selectedRow.name + "]当前已处于退学状态!", "error");
            } else if (selectedRow.status == 0) {
                $.messager.confirm("提示", "该同学确定要退学吗?", function (yes) {
                    if (yes) {
                        $.post("/regularStu/removeStu", {id: selectedRow.id}, function (data) {
                            if (data.success) {
                                $.messager.alert("提示", "退学成功", "info", function () {
                                    cmdObj.cancel();
                                    cmdObj.reloadRegularStu();
                                })
                            } else {
                                $.messager.alert("提示", data.msg, "error");
                            }
                        });
                    }
                });
            } else {
                $.messager.alert("提示", "请先退出当前状态再试", "error");
            }
        },
        //休学
        restStudyRegularStu: function () {
            //获取选中行
            var selectedRow = regularStu_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "当前无人被选中", "error");
                return;
            }
            //判断当前学员状态
            if (selectedRow.status == -1) {
                $.messager.confirm("提示", "该同学确定要退出休学状态吗?", function (yes) {
                    if (yes) {
                        $.post("/regularStu/recoverStu", {id: selectedRow.id}, function (data) {
                            if (data.success) {
                                $.messager.alert("提示", "恢复成功", "info", function () {
                                    cmdObj.cancel();
                                    cmdObj.reloadRegularStu();
                                })
                            } else {
                                $.messager.alert("提示", data.msg, "error");
                            }
                        })
                    }
                })
            } else if (selectedRow.status == 0) {
                $.messager.confirm("提示", "该同学确定要休学吗?", function (yes) {
                    if (yes) {
                        $.post("/regularStu/restStu", {id: selectedRow.id}, function (data) {
                            if (data.success) {
                                $.messager.alert("提示", "休学成功", "info", function () {
                                    cmdObj.cancel();
                                    cmdObj.reloadRegularStu();
                                })
                            } else {
                                $.messager.alert("提示", data.msg, "error");
                            }
                        })
                    }
                })
            } else {
                $.messager.alert("提示", "请先退出当前状态再试", "error");
            }
        },
        //查看
        seeRegularStu: function () {
            //获取选中行
            var selectedRow = regularStu_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "当前无人被选中", "error");
                return;
            }
            regularStu_add_form.form('clear');
            //数据回显

            //性别回显
            if (selectedRow.gender == 0) {
                $("#gender_combobox").combobox("select", "0");
            } else {
                $("#gender_combobox").combobox("select", "1");
            }
            //所在班级回显
            if (selectedRow.classgrade) {
                selectedRow['classgrade.id'] = selectedRow.classgrade.id;
            }
            //营销人员回显
            if (selectedRow.inputEmployee) {
                selectedRow['inputEmployee.id'] = selectedRow.inputEmployee.id;
            }
            //所在校区回显
            if (selectedRow.campus) {
                selectedRow['campus.id'] = selectedRow.campus.id;
            }
            //所在学院回显
            if (selectedRow.college) {
                selectedRow['college.id'] = selectedRow.college.id;
            }
            //付款方式回显
            if (selectedRow.payment) {
                selectedRow['payment.id'] = selectedRow.payment.id;
            }
            //支付类型回显
            if (selectedRow.paymentType) {
                selectedRow['paymentType.id'] = selectedRow.paymentType.id;
            }
            regularStu_dialog.dialog('setTitle', '查看正式学员资料');
            regularStu_add_form.form('load', selectedRow);
            $(".easyui-combobox").combobox('readonly', true);
            $(".easyui-datebox").datebox('readonly', true);
            $(".easyui-textbox").textbox('readonly', true);
            $("#commit_button").linkbutton('disable');
            regularStu_dialog.dialog('open');
        },
        //升班
        promoteRegularStu: function () {
            //获取选中行
            var selectedRow = regularStu_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "当前无人被选中", "error");
                return;
            }
            if (selectedRow.classStatus == 0 && selectedRow.status == 0) {
                //弹出确认框
                $.messager.confirm("提示", "当前学员确定具有升班资格吗?", function (yes) {
                    if (yes) {
                        $.post("/regularStu/promote", {
                            id: selectedRow.id,
                            status: selectedRow.classStatus
                        }, function (data) {
                            if (data.success) {
                                $.messager.alert("提示", "升班成功", "info", function () {
                                    cmdObj.cancel();
                                    cmdObj.reloadRegularStu();
                                })
                            } else {
                                $.messager.alert("提示", data.msg, "error");
                            }
                        })
                    }
                })
            } else if (selectedRow.classStatus == 1) {
                if (selectedRow.status == 0) {
                    $.messager.alert("提示", "当前学员已经是大神班的一员啦", "info");
                } else if (selectedRow.status == 1) {
                    $.messager.alert("提示", "当前学员已经毕业啦", "info");
                } else if (selectedRow.status == -1) {
                    $.messager.alert("提示", "当前学员正在休学中", "info");
                } else if (selectedRow.status == -2) {
                    $.messager.alert("提示", "当前学员已经退学啦", "info");
                }
            } else if (selectedRow.classStatus == 0) {
                if (selectedRow.status == 0) {
                    //如果班级状态不是基础班或者不具备升班条件
                    $.messager.alert("提示", "请确认当前学员是否具有升班资格", "error");
                } else if (selectedRow.status == -1) {
                    $.messager.alert("提示", "当前学员正在休学中", "info");
                } else if (selectedRow.status == -2) {
                    $.messager.alert("提示", "当前学员已经退学啦", "info");
                }
            }
        },
        //毕业
        endRegularStu: function () {
            //获取选中行
            var selectedRow = regularStu_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "当前无人被选中", "error");
                return;
            }
            if (selectedRow.classStatus == 1 && selectedRow.status == 0) {
                $.messager.confirm("提示", "当前学员确定已经完成学业任务了吗?", function (yes) {
                    if (yes) {
                        console.log(selectedRow);
                        if (selectedRow.feeStatus == 1) {
                            $.post("/regularStu/endRegularStu", {id: selectedRow.id}, function (data) {
                                if (data.success) {
                                    $.messager.alert("提示", "毕业快乐!", "info", function () {
                                        cmdObj.cancel();
                                        cmdObj.reloadRegularStu();
                                    });
                                } else {
                                    $.messager.alert("提示", data.msg, "error");
                                }
                            });
                        } else {
                            $.messager.alert("提示", "当前学员学费仍未缴清,请缴清后重试", "error");
                        }
                    }
                });
            } else if (selectedRow.classStatus == 0 && selectedRow.status == 0) {
                $.messager.alert("提示", "请当前学员完成未完成的学业任务", "error");
            } else if (selectedRow.classStatus == 1 && selectedRow.status == 1) {
                $.messager.alert("提示", "已经毕业的学员不能再进行此操作", "error");
            } else {
                $.messager.alert("提示", "当前学员不满足毕业的条件", "error");
            }
        },
        //留级
        retentionRegularStu: function () {
            //获取选中行
            var selectedRow = regularStu_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "当前无人被选中", "error");
                return;
            }
            if (selectedRow.classStatus == 1 && selectedRow.status == 0) {
                $.messager.confirm("提示", "当前[" + selectedRow.name + "]学员确定要留级吗?", function (yes) {
                    if (yes) {
                        retention_dialog.dialog({
                            buttons: "#retention_buttons"
                        });
                    }
                });
            } else if (selectedRow.classStatus == 0) {
                $.messager.alert("提示", "该学员已经在基础班,无法留级", "error");
            } else {
                $.messager.alert("提示", "请检查该学员当前状态再试", "error");
            }
        },
        //取消留级
        cancel_retention: function () {
            //关闭留级弹窗
            retention_dialog.dialog("close");
        },
        //保存留级信息
        save_retention: function () {
            var selectedRow = regularStu_datagrid.datagrid("getSelected");
            var retentionClassradeId = $("#retentionClassrade").val();

            $.messager.confirm("提示", "该学员确定留级在该班级吗?", function (yes) {
                if (yes) {
                    $.post("/regularStu/retentionRegularStu", {
                        id: selectedRow.id,
                        classgradeId: retentionClassradeId
                    }, function (data) {
                        if (data.success) {
                            $.messager.alert("提示", "留级成功", "info", function () {
                                cmdObj.cancel_retention();
                                cmdObj.reloadRegularStu();
                            });
                        } else {
                            $.messager.alert("提示", data.msg, "error");
                        }
                    });
                }
            });
        }
    }


    //2.按钮的点击事件通过js来绑定
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });


});

//格式化营销人员
function formatterInputEmployee(value, row, index) {
    return value == null ? "" : value.realName;
}

//格式化费用状态
function formatterFeeStatus(value, row, index) {
    if (value == 0) {
        return "<span style='color: red'>未缴清</span>";
    } else {
        return "已缴清";
    }
}

//格式化学员状态
function formatterStatus(value, row, index) {
    if (value == 0) {
        return "正常";
    } else if (value == 1) {
        return "已毕业";
    } else if (value == -1) {
        return "<span style='color: #d9534f'>休学中</span>";
    } else if (value == -2) {
        return "<span style='color: #d9534f'>已退学</span>";
    }
}

//格式化性别
function formatterGender(value, row, index) {
    if (value == 0) {
        return "男";
    } else {
        return "女";
    }
}

//格式化所在班级
function formatterClassGrade(value, row, index) {
    return value == null ? "" : value.name;
}

//格式化班级状态
function formatterClassStatus(value, row, index) {
    if (value == 0) {
        return "基础班";
    } else if (value == 1) {
        return "<span style='color: #d9534f'>大神班</span>";
    }
}

//格式化所在学院
function formatterCollege(value, row, index) {
    return value == null ? "" : value.name;
}

//格式化付款方式
function formatterPayment(value, row, index) {
    return value == null ? "" : value.name;
}

//格式化支付类型
function formatterPaymentType(value, row, index) {
    return value == null ? "" : value.name;
}

//格式化所在校区
function formatterCampus(value, row, index) {
    return value == null ? "" : value.name;
}